package hr.bean;

import java.util.ArrayList;
import java.util.Date;

import hr.dao.DepartmentDAO;
import hr.dao.DesignationDAO;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import resources.bean.Login;
import resources.pojo.hr_designation;
import resources.util.Tool;

@ManagedBean(name="designation")
@SessionScoped
public class Designation {
	private int currentPage;
	private hr_designation designation;

	private hr_designation selectedDesignation;
	private String nameSearch;
	private ArrayList<hr_designation> listDesignation;
	private Integer[] pages;
	private String last;
	
	public void onload() {
		if(!Tool.isPostBack()){
			init();
		}
	}
	
	public void initObject(){
		if(!Tool.isPostBack()){
			designation = new hr_designation();
		}
	}
	
	public void init(){
		currentPage = 1;
		nameSearch="";
		designation = new hr_designation();
		setListDesignation(DesignationDAO.getListDesignationPaging("",currentPage,Tool.max_row));
		setPages(Tool.getPages());
	}
	
	public void paging(){
		setListDesignation(DesignationDAO.getListDesignationPaging("",currentPage,Tool.max_row));
		setPages(Tool.getPages());
	}
	
	public void insert(){
		designation.setId("DES" + Tool.generateID());
		designation.setStatus(1);
		String result = DesignationDAO.action("insert",designation,0);
		if(result.equals("completed")){
		}else if(result.equals("duplicate")){
			Tool.message("Tên phòng ban không được trùng nhau");
		}
		init();
	}
	
	public void update(){
		String result = DesignationDAO.action("update",selectedDesignation,last.equals(selectedDesignation.getName())?1:0);
		if(result.equals("completed")){
		}else if(result.equals("duplicate")){
			Tool.message("Tên phòng ban không được trùng nhau");
		}
		init();
	}
	
	public void choose(ActionEvent event){
		selectedDesignation = (hr_designation) event.getComponent().getAttributes().get("object");
		last = selectedDesignation.getName();
	}
	
	public void search(){
		setListDesignation(DesignationDAO.getListDesignationPaging(nameSearch,1,Tool.max_row));
		setPages(Tool.getPages());
	}
	
	public void nextPage(){
		currentPage ++;
	}

	public String getNameSearch() {
		return nameSearch;
	}

	public void setNameSearch(String nameSearch) {
		this.nameSearch = nameSearch;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setPages(Integer[] pages) {
		this.pages = pages;
	}

	public Integer[] getPages() {
		return pages;
	}

	public void setListDesignation(ArrayList<hr_designation> listDesignation) {
		this.listDesignation = listDesignation;
	}

	public ArrayList<hr_designation> getListDesignation() {
		return listDesignation;
	}

	public hr_designation getDesignation() {
		return designation;
	}

	public void setDesignation(hr_designation designation) {
		this.designation = designation;
	}

	public hr_designation getSelectedDesignation() {
		return selectedDesignation;
	}

	public void setSelectedDesignation(hr_designation selectedDesignation) {
		this.selectedDesignation = selectedDesignation;
	}
}
